package com.xinda.admin.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xinda.admin.pojo.ServiceOrder;
import com.xinda.admin.service.OrderService;
import com.xinda.admin.vo.OrderEditVo;
import com.xinda.admin.vo.OrderListVo;

@Controller
public class OrderController {
	
	@Autowired
	private OrderService orderService;
	
	/*
	 * 订单列表
	 */
	@RequestMapping(value = "orderList")
	public String orderList(HttpServletRequest request, Model model) {
        
		String type = request.getParameter("type");
		String mindate = request.getParameter("datemin");
		String maxdate = request.getParameter("datemax");
		String orderNo = request.getParameter("orderNo");
		
		Map<String, String> searchif = new HashMap<String, String>();
		searchif.put("type", type);
		searchif.put("mindate", mindate);
		searchif.put("maxdate", maxdate);
		searchif.put("orderNo", orderNo);
		
		List<OrderListVo> listOrder = orderService.queryOrderByStatus(searchif);
		
		model.addAttribute("type", type);
		model.addAttribute("count", listOrder.size());
		model.addAttribute("listOrder", listOrder);
		
		return "admin/order/orderList";
	}
	
	@RequestMapping(value = "orderUpdateOfStatus")
	@ResponseBody
	public void orderUpdateOfStatus(HttpServletRequest request){
		
		int id = Integer.valueOf(request.getParameter("id"));
		int status = Integer.valueOf(request.getParameter("status"));
		ServiceOrder serviceOrder = new ServiceOrder();
		serviceOrder.setId(id);
		serviceOrder.setStatus(status);
		
		orderService.updateOrderStatusById(serviceOrder);
	}
	
	@RequestMapping(value = "orderDetail")
	public String orderEdit(HttpServletRequest request, Model model) {
		int id = Integer.valueOf(request.getParameter("id"));
		OrderEditVo orderEditVo = new OrderEditVo();
		orderEditVo = orderService.queryQrderEditVoById(id);
		model.addAttribute("orderEditVo", orderEditVo);
		return "admin/order/orderDetail";
	}
	
	@RequestMapping(value = "closeOrders")
	@ResponseBody
	public void closeOrders(int[] orderCheckBox) {
		List<Integer> ids = new ArrayList<Integer>();
		for (int i : orderCheckBox){
			ids.add(i);
		}
		OrderListVo orderlistvo = new OrderListVo();
		orderlistvo.setIds(ids);
		orderService.closeOrdersStatusByIds(orderlistvo);
	}
	
	@RequestMapping(value = "orderUpdateOfTotalPrice")
	@ResponseBody
	public void orderUpdateOfTotalPrice(OrderEditVo orderEditVo){
		orderService.updateOrderTotalPriceByOrderEditVo(orderEditVo);
	}
}
